//
//  UIView+Frame.m
//  Extision
//
//  Created by 岳道峰 on 2017/4/2.
//  Copyright © 2017年 hao. All rights reserved.
//

#import "UIView+Frame.h"

@implementation UIView (Frame)

- (BOOL)isShowingOnKeyWindow
{
    // 主窗口
    UIWindow *keyWindow = [UIApplication sharedApplication].keyWindow;
    
    // 以主窗口左上角为坐标原点, 计算self的矩形框
    CGRect newFrame = [keyWindow convertRect:self.frame fromView:self.superview];
    CGRect winBounds = keyWindow.bounds;
    
    // 主窗口的bounds 和 self的矩形框 是否有重叠
    BOOL intersects = CGRectIntersectsRect(newFrame, winBounds);
    
    return !self.isHidden && self.alpha > 0.01 && self.window == keyWindow && intersects;
}

+ (instancetype)viewFromXib:(Class)className
{
    return [[NSBundle mainBundle] loadNibNamed: NSStringFromClass(className) owner:self options:nil].firstObject;
}

+ (instancetype)viewFromXibName:(NSString *)name
{
    return [[NSBundle mainBundle] loadNibNamed:name owner:self options:nil].firstObject;
}

- (CGSize)size
{
    return CGSizeMake(self.width, self.height);
}

- (void)setSize:(CGSize)size
{
    CGRect frame = self.frame;
    frame.size = size;
    self.frame = frame;
}

- (CGFloat)width
{
    CGFloat w = self.bounds.size.width;
    if (w == 0) {
        w = [self systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].width;
    }
    return w;
}

- (void)setWidth:(CGFloat)width
{
    CGRect frame = self.frame;
    frame.size.width = width;
    self.frame = frame;
}

- (CGFloat)height
{
    CGFloat h = self.bounds.size.height;
    if (h == 0) {
        h = [self systemLayoutSizeFittingSize:UILayoutFittingCompressedSize].height;
    }
    return h;
}

- (void)setHeight:(CGFloat)height
{
    CGRect frame = self.frame;
    frame.size.height = height;
    self.frame = frame;
}

- (CGFloat)x
{
    return self.frame.origin.x;
}

- (void)setX:(CGFloat)x
{
    CGRect frame = self.frame;
    frame.origin.x = x;
    self.frame = frame;
}

- (CGFloat)y
{
    return self.frame.origin.y;
}

- (void)setY:(CGFloat)y
{
    CGRect frame = self.frame;
    frame.origin.y = y;
    self.frame = frame;
}

- (CGFloat)center_x
{
    return self.center.x;
}

- (void)setCenter_x:(CGFloat)center_x
{
    CGPoint center = self.center;
    center.x = center_x;
    self.center = center;
}

- (CGFloat)center_y
{
    return self.center.y;
}

- (void)setCenter_y:(CGFloat)center_y
{
    CGPoint center = self.center;
    center.y = center_y;
    self.center = center;
}

- (CGFloat)max_x
{
    return CGRectGetMaxX(self.frame);
}

- (CGFloat)max_y
{
    return CGRectGetMaxY(self.frame);
}

- (CGFloat)min_x
{
    return CGRectGetMinX(self.frame);
}

- (CGFloat)min_y
{
    return CGRectGetMinY(self.frame);
}

- (CGFloat)bounds_x
{
    return self.bounds.origin.x;
}

- (void)setBounds_x:(CGFloat)bounds_x
{
    CGRect bounds = self.bounds;
    bounds.origin.x = bounds_x;
    self.bounds = bounds;
}

- (CGFloat)bounds_y
{
    return self.bounds.origin.y;
}

- (void)setBounds_y:(CGFloat)bounds_y
{
    CGRect bounds = self.bounds;
    bounds.origin.y = bounds_y;
    self.bounds = bounds;
}

@end
